import re

import pymssql
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
from Parse_page import Parse_page

class JDBDS(object):
    def __init__(self):
        self.key_word = ['芝华仕（CHEERS）', 'A家', '顾家家居（KUKA）', '全友（QUANU）', '梦美斯宣', '慕尼思丹（munisidan）',\
                         '丽巢', '中派', '柏曼诗（BOMENSSI）', '作木坊', '梵爱（VASO LOVE）', '左右', '亲友（qinyou）', '滑氏红木',\
                        '卡貝森', '乔伦公馆', '欧伦图（OULUNTU）', '摩天（Motian）', '林氏木业', '皇系（HUANGXI）']
        options = webdriver.ChromeOptions()
        # options.add_argument('--headless')
        options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 隐藏程序模拟浏览器
        self.driver = webdriver.Chrome(chrome_options=options)
        self.wait = WebDriverWait(self.driver, 10)
        self.db = pymssql.connect(host='192.168.0.186', user='sa', password='987987abc#', port='1433',
                                  database='Crawler.Paterson', charset="utf8")
        self.pp = Parse_page(self.driver, self.db)

    def Bed(self, key):
        first_category = '床'
        second_category = key
        self.url_bed = 'https://search.jd.com/search?keyword='+str(key)+'&enc=utf-8&qrst=1&rt=1&stop=1&vt=2\
                        &stock=1&cid3=9863#J_searchWrap'.replace(' ', '')
        print(self.url_bed.replace(' ', ''))
        self.driver.get(self.url_bed)
        return first_category, second_category

    def Sofa(self, key):
        first_category = '沙发'
        second_category = key
        self.url_sofa = 'https://search.jd.com/search?keyword='+str(key)+'&enc=utf-8&qrst=1\
                    &rt=1&stop=1&vt=2&wq='+str(key)+'&stock=1&cid3=9870#J_searchWrap'.replace(' ', '')
        print(self.url_sofa.replace(' ', ''))
        self.driver.get(self.url_sofa)
        return first_category, second_category

    def get_page_num(self):
        try:
            page_num = self.driver.find_element_by_xpath('//*[@id="J_bottomPage"]/span[2]/em[1]/b')
            page_num = [page_num.text][0]
        except:
            page_num = 1
        finally:
            return page_num

    def turn_page(self, page):
        button = self.wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="J_bottomPage"]/span[2]/input')))
        button.clear()
        button.send_keys(page)
        self.wait.until(EC.element_to_be_clickable((By.XPATH,  '//*[@id="J_bottomPage"]/span[2]/a'))).click()

    def Main(self, control):
        for i in self.key_word:
            if control == 1:
                first_category, second_category = self.Bed(i)
            if control == 2:
                first_category, second_category = self.Sofa(i)
            time.sleep(3)
            page_num = int(self.get_page_num())
            print(page_num)
            if page_num == 1:
                print(page_num)
                time.sleep(1)
                self.pp.parse_page(first_category, second_category)
            elif page_num >= 2:
                print('解析')
                self.pp.parse_page(first_category, second_category)
                for ii in range(2, page_num+1):
                    print(ii)
                    self.pp.parse_page(first_category, second_category)
                    self.turn_page(ii)
                    time.sleep(1+ii/2)
        time.sleep(5)
        self.db.close()


if __name__ == '__main__':
    jd = JDBDS()
    jd.Main(1)
